﻿#region namespaces

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Facebook.Rest;
using Facebook.Session;
using Facebook.Schema;
#endregion

namespace SLIITPro.Controls
{
    public partial class HomeMenu : System.Web.UI.UserControl
    {
        private Api _facebookApi;
        private ConnectSession _connectSession;
        protected void Page_Load(object sender, EventArgs e)
        {
            _connectSession = new ConnectSession(ConfigurationManager.AppSettings["FBApiKey"],
                                                ConfigurationManager.AppSettings["FBSecret"]);


            if (Page.User.Identity.IsAuthenticated)
            {
                lblUser.Text = string.Format("&nsub; Welcome {0} you are logged in as a {1}", Page.User.Identity.Name, GetRoles());
            }

            else if (_connectSession.IsConnected())
            {
                _facebookApi = new Api(_connectSession);
                Facebook.Schema.user user = _facebookApi.Users.GetInfo();
                lblUser.Text = "Welcome " + user.first_name + " " + user.last_name;
            }
            else
            {
                lblUser.Text = "&nsub; Welcome Anonymous";
            }
        }

        private string GetRoles()
        {
            if (Page.User.IsInRole("administrator")) return "Administrator";
            if (Page.User.IsInRole("information")) return "Information User";
            if (Page.User.IsInRole("content")) return "Content User";
            if (Page.User.IsInRole("business")) return "Business User";
            return string.Empty;
        }

    }
}